package vsqlbuilder.logic.sql;

import java.util.ArrayList;
import java.util.List;

import vsqlbuilder.logic.Table;

public class QueryTable {
	private Table baseTable;
	private List<QueryColumn> includeColumns;

	public QueryTable(Table table) {
		this.baseTable = table;
		this.includeColumns = new ArrayList<QueryColumn>();
	}

	public String getTableName() {
		return this.baseTable.getName();
	}

	public String getOwnerSchema() {
		return this.baseTable.getSchemaName();
	}

	public int getNumberCols() {
		return this.includeColumns.size();
	}

	public String getColumnNameAt(int index) {
		return this.includeColumns.get(index).data.getName();
	}

	public String getColumnDatatypeAt(int index) {
		return this.includeColumns.get(index).data.getDataType();
	}

	public void addColumnToQuery(QueryColumn qcolumn) {
		this.includeColumns.add(qcolumn);
	}

	public void removeColumnFromQuery(String columnName) {
		if (this.includeColumns.size() > 1) {
			for (QueryColumn qc : this.includeColumns) {
				if (qc.data.getName().equals(columnName)) {
					this.includeColumns.remove(qc);
					break; // [BUG FIX#5] 1/3 if continue iterating over the
							// modified list a exception is throw when selecting
							// or unselecting columns for a query.
				}
			}
		} else {
			this.includeColumns.clear();
		}
	}

	// Remove all columns from this query table object [but before unselect all
	// his columns]
	public void removeAllColumnsFromQuery() {
		for (QueryColumn qc : this.includeColumns) {
			qc.unselectQueryColumn();
		}
		this.includeColumns.clear();
	}

}
